#include "utilities.h"

void deleteProcessing(msg_header msgRespHdr, listBuffStruct eventReq)
{
	map<int,writeListData>::iterator it;
	//Process the Store request accordingly
	if((uint8_t)msgRespHdr.msgtype == DELT)
	{
		if(printFlag == 1)
		{
			cout<<"got delete msg"<<endl;
		}
		int testUOID = findInUOIDList(msgRespHdr.uoid);
		if(testUOID == 1)
		{
			
		}
		else
		{
			uoidStr tempUOIDStr;
			struct timeval time1;
			gettimeofday(&time1, NULL);
			tempUOIDStr.timeOfTheDay = (double)(time1.tv_sec * (1e+3) + (double)time1.tv_usec/1000);
			tempUOIDStr.msgLifeTime = initMap["MsgLifetime"];
			memcpy(tempUOIDStr.msgUOID, msgRespHdr.uoid, 20);
			pthread_mutex_lock(&uoidlock);
			myUOIDList.push_back(tempUOIDStr);
			pthread_mutex_unlock(&uoidlock);
			char filename[200];
			memset(filename,'\0',200);
			memcpy(filename,&(eventReq.listBuff[36]),msgRespHdr.datalength-95);
			if(printFlag == 1)
			{
				cout<<msgRespHdr.datalength<<endl;
				cout<<filename<<endl;
			}
			char nonce[20],filesha1[20],password[200];
			memcpy(filesha1,&(eventReq.listBuff[36+7+msgRespHdr.datalength-95]),20);
			memcpy(nonce,&(eventReq.listBuff[64+7+msgRespHdr.datalength-95]),20);
			memcpy(password,&(eventReq.listBuff[95+7+msgRespHdr.datalength-95]),20);
			password[20]='\0';	
			list<storeListStr>::iterator itF;
			char storefilename[512];
			FILE *pFile;
			pthread_mutex_lock(&storelock);
			for(itF=myStoreList.begin(); itF!=myStoreList.end(); itF++)
			{
				storeListStr storeTemp = *itF;
				if(strcmp(storeTemp.fileName,filename)==0)
				{
					int index = storeTemp.fileIndex;
					if(printFlag == 1)
					{
						cout<<"File Found"<<endl;
						cout<<index<<endl;
					}
					char deleteMessage[8192];
					char tempMessage[200];
					memset(deleteMessage,'\0',8192);
					strcpy(tempMessage,"FileName=");
					memcpy(&(deleteMessage[0]),tempMessage,9);
					memcpy(&(deleteMessage[9]),storeTemp.fileName,strlen(storeTemp.fileName));
					strcpy(tempMessage,"\r\n");
					memcpy(&(deleteMessage[9+strlen(storeTemp.fileName)]),tempMessage,2);
					strcpy(tempMessage,"SHA1=");
					memcpy(&(deleteMessage[11+strlen(storeTemp.fileName)]),tempMessage,5);
					memcpy(&(deleteMessage[16+strlen(storeTemp.fileName)]),storeTemp.fileSha1,20);
					strcpy(tempMessage,"\r\n");
					memcpy(&(deleteMessage[36+strlen(storeTemp.fileName)]),tempMessage,2);
					strcpy(tempMessage,"Nonce=");
					memcpy(&(deleteMessage[38+strlen(storeTemp.fileName)]),tempMessage,6);
					memcpy(&(deleteMessage[44+strlen(storeTemp.fileName)]),storeTemp.fileNonce,20);
					strcpy(tempMessage,"\r\n");
					memcpy(&(deleteMessage[64+strlen(storeTemp.fileName)]),tempMessage,2);
					strcpy(tempMessage,"Password=");
					memcpy(&(deleteMessage[66+strlen(storeTemp.fileName)]),tempMessage,9);
					char fc[10];
					memset(fc,'\0',10);
					sprintf(fc,"%d",index);
					char noncebuf[SHA_DIGEST_LENGTH];
					SHA1((const unsigned char*)password, strlen(password),(unsigned char*)noncebuf);
					if(printFlag == 1)
					{	
					for(int ii=0;ii<20;ii++)
					printf("%02x->%02x\n",(uint8_t)noncebuf[ii],(uint8_t)storeTemp.fileNonce[ii]);
					}
					if(memcmp(noncebuf,storeTemp.fileNonce,20)==0)
					{
						if(printFlag == 1)
						{
							cout<<"Match Found Deleting File"<<endl;
						}
						memcpy(&(deleteMessage[75+strlen(storeTemp.fileName)]),password,20);
						strcpy(storefilename,initMap["HomeDir"].asStr);
						strcat(storefilename,"/");
						strcat(storefilename,"files");
						strcat(storefilename,"/");
						strcat(storefilename,fc);
						char deletefilename[8192];
						memset(deletefilename,'\0',8192);
						strcpy(deletefilename,storefilename);
						strcat(deletefilename,".data");
						if( remove(deletefilename) != 0 )
						perror( "Error deleting file" );
						else
						{
							cout<<"File Has Been Deleted"<<endl;
							cout<<"servant:"<<port_recieve<<"> ";
							fflush(stdout);
							if(printFlag == 1)
							{
							printf("%s.data File successfully deleted\n",fc );
							}
						}
						memset(deletefilename,'\0',8192);
						strcpy(deletefilename,storefilename);
						strcat(deletefilename,".meta");
						if( remove(deletefilename) != 0 )
						{
							perror( "Error deleting file" );
						}
						else
						{
							if(printFlag == 1)
							{
								printf("%s.meta File successfully deleted\n",fc );
							}
						}
						if(printFlag == 1)
						{
							cout<<myStoreList.size()<<endl;
						}
						itF = myStoreList.erase(itF);
						pthread_mutex_lock(&searchFileIdsLock);
						list<search_fileId_results>::iterator itSeFileId;
						for(itSeFileId=searchFileIdsList.begin(); itSeFileId!=searchFileIdsList.end(); itSeFileId++)
						{
							search_fileId_results temp = *itSeFileId;
							if(temp.fileIndex == storeTemp.fileIndex)
							{
								searchFileIdsList.erase(itSeFileId);
								break;
							}
						}
						pthread_mutex_unlock(&searchFileIdsLock);
						if(printFlag == 1)
						{
							cout<<myStoreList.size()<<endl;
						}
					}	
					listBuffStruct lbtemp;
					lbtemp.logtype = 1;
					memset(lbtemp.listBuff, '\0', 8192);				
					msgRespHdr.timetolive--;
					fillMsgHdrBuffer(msgRespHdr, lbtemp.listBuff);
					memcpy(&(lbtemp.listBuff[27]), deleteMessage,95+strlen(storeTemp.fileName));
					lbtemp.datalength = 27 + 95 + strlen(storeTemp.fileName);
					pthread_mutex_lock(&writeMapMutex);
					for(it=writeMap.begin(); it!=writeMap.end(); it++)
					{
						if((*it).first!=eventReq.portNumber)
						{
							lbtemp.portNumber = (*it).first;
							writeListData listDataStr = writeMap[(*it).first];
							listDataStr.response.push_back(lbtemp);
							writeMap[(*it).first] = listDataStr;
						}
					}
					pthread_mutex_unlock(&writeMapMutex);
					}
			}
			strcpy(storefilename,initMap["HomeDir"].asStr);
			strcat(storefilename,"/");
			strcpy(filename,storefilename);
			strcat(filename,"kwrd_index");
			pFile=fopen(filename,"w");
			fclose(pFile);
			strcpy(filename,storefilename);
			strcat(filename,"sha1_index");
			pFile=fopen(filename,"w");
			fclose(pFile);
			strcpy(filename,storefilename);
			strcat(filename,"nonce_index");
			pFile=fopen(filename,"w");
			fclose(pFile);
			strcpy(filename,storefilename);
			strcat(filename,"name_index");
			pFile=fopen(filename,"w");
			fclose(pFile);
			if(printFlag == 1)
			{
				cout<<myStoreList.size()<<endl;
			}
			for(itF=myStoreList.begin(); itF!=myStoreList.end(); itF++)
			{
				if(printFlag == 1)
				{
					cout<<"enters"<<endl;
				}
				storeListStr storeData = *itF;
				strcpy(filename,initMap["HomeDir"].asStr);
				strcat(filename,"/");
				strcat(filename,"kwrd_index");
				pFile=fopen(filename,"a");
				fprintf(pFile,"%d=",storeData.fileIndex);
				int j =0;
				for(j=0;j<256;j++)
				{
					fprintf(pFile,"%c",storeData.fileBitVector[j]);
				}
				fprintf(pFile,"\n");
				fclose(pFile);

				//Create the name_index file
				strcpy(filename,initMap["HomeDir"].asStr);
				strcat(filename,"/");
				strcat(filename,"name_index");
				pFile=fopen(filename,"a");
				fprintf(pFile,"%d=",storeData.fileIndex);
				fprintf(pFile,"%s",storeData.fileName);
				fprintf(pFile,"\n");
				fclose(pFile);

				//Create the nonce_index file
				strcpy(filename,initMap["HomeDir"].asStr);
				strcat(filename,"/");
				strcat(filename,"nonce_index");
				pFile=fopen(filename,"a");
				fprintf(pFile,"%d=",storeData.fileIndex);
				for(j=0;j<20;j++)
				{
					fprintf(pFile,"%02x",(uint8_t)storeData.fileNonce[j]);
				}
				fprintf(pFile,"\n");
				fclose(pFile);

				//Create the sha1_index file
				strcpy(filename,initMap["HomeDir"].asStr);
				strcat(filename,"/");
				strcat(filename,"sha1_index");
				pFile=fopen(filename,"a");
				fprintf(pFile,"%d=",storeData.fileIndex);
				for(j=0;j<20;j++)
				{
					fprintf(pFile,"%02x",(uint8_t)storeData.fileSha1[j]);
				}
				fprintf(pFile,"\n");
				fclose(pFile);	
			}
			pthread_mutex_unlock(&storelock);
		}
	}
}
